'use strict';
function hexToNumber(data){
    return data[0]*256*256*256+data[1]*256*256+data[2]*256+data[3];
}
//Start by defining the main module and adding the module dependencies
angular.module(ApplicationConfiguration.applicationModuleName, ApplicationConfiguration.applicationModuleVendorDependencies);

// Setting HTML5 Location Mode
angular.module(ApplicationConfiguration.applicationModuleName).config(['$locationProvider', '$httpProvider',
    function($locationProvider, $httpProvider) {
        $locationProvider.html5Mode(true).hashPrefix('!');

        $httpProvider.interceptors.push('AuthInterceptor');
    }
]);

angular.module(ApplicationConfiguration.applicationModuleName).run([
    '$rootScope', '$state', '$cookies', '$location', 'Initialization', 'Authentication', 'ExtDialog', 
    function($rootScope, $state, $cookies, $location, Initialization, Authentication, ExtDialog) {
        //jquery1.9 support,以下browser
        angular.element.browser = angular.element.support;
        window.addEventListener("message", receiveMessage, false);
        function receiveMessage(event)
        {
          var origin = event.origin || event.originalEvent.origin; // For Chrome, the origin property is in the event.originalEvent object.
          console.log(event.data);
          $rootScope.$broadcast('serial',event.data);
          
        }
        $rootScope.$on('$stateChangeStart', function(event, nextRoute, currentRoute) {
            if (nextRoute != null && (nextRoute.access != null && nextRoute.access.requiredAuthentication) &&
                !Authentication.isAuthenticated && !$cookies.get('U9USS')
            ) {
                $location.path('/User/Login');
            }
            if (nextRoute != null && nextRoute.access != null) {
                if (nextRoute.name === 'index.home' ||
                    nextRoute.name === 'index.ChangePassword'
                ) {
                    $rootScope.$broadcast('subMenuChange', {
                        Name: nextRoute.access.name,
                        notRolesMenu: true
                    });
                }
            }
        });

        $rootScope.$on('responseError', function(event, rejection) {
            if (rejection.status === -1) {
                ExtDialog.tip('连接超时，请稍后重试');
            } else {
                if (angular.isObject(rejection) && rejection.desc) {
                    ExtDialog.tip(rejection.desc);
                }
            }
        });

        // Record previous state
        $rootScope.$on('$stateChangeSuccess', function(event, toState, toParams, fromState, fromParams) {
            storePreviousState(fromState, fromParams);
        });

        // Store previous state
        function storePreviousState(state, params) {
            // only store this state if it shouldn't be ignored 
            if (!state.data || !state.data.ignoreState) {
                $state.previous = {
                    state: state,
                    params: params,
                    href: $state.href(state, params)
                };
            }
        }
    }
]);

//Then define the init function for starting up the application
angular.element(document).ready(function() {
    //Fixing facebook bug with redirect
    if (window.location.hash && window.location.hash === '#_=_') {
        if (window.history && history.pushState) {
            window.history.pushState('', document.title, window.location.pathname);
        } else {
            // Prevent scrolling by storing the page's current scroll offset
            var scroll = {
                top: document.body.scrollTop,
                left: document.body.scrollLeft
            };
            window.location.hash = '';
            // Restore the scroll offset, should be flicker free
            document.body.scrollTop = scroll.top;
            document.body.scrollLeft = scroll.left;
        }
    }

    //Then init the app
    angular.bootstrap(document, [ApplicationConfiguration.applicationModuleName]);
});
